package com.common.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class RedisUtils {
    @Autowired
    private  RedisTemplate redisTemplate;

    /**\
     * 根据key删除
     * @param key
     */
    public void remove(String... key){
        if(key!=null && key.length>0){
            if(key.length==1){
                redisTemplate.delete(key[0]);
            }
            else{
                redisTemplate.delete(Arrays.asList(key));
            }
        }
    }

    /**
     * 添加有序集合
     * @param key
     * @param data
     * @param score
     * @param time
     */
    public void zAdd(String key,Object data,double score,long time){
        redisTemplate.opsForZSet().add(key,data,score);
        this.expire(key,time);
    }

    /**
     * 给key设置过期时间
     * @param key
     * @param time
     * @return
     */
    public boolean expire(String key,long time){
        try {
            if (time > 0) {
                redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("放入redis出错了");

            return false;
        }
    }

    /**
     * 普通存入缓存，没有过期时间
     * @param ket
     * @param data
     * @return
     */
    public boolean set(String ket,Object data){
        try{
            redisTemplate.opsForValue().set(ket,data);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("放入redis出错了");

            return false;
        }
    }

    /**
     * 根据过期时间time存入缓存
     * @param key
     * @param data
     * @param time
     * @return
     */
    public boolean set(String key,Object data,long time){
        try{
            if(time>0){
                redisTemplate.opsForValue().set(key,data,time, TimeUnit.SECONDS);
            }else{
                set(key, data);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("放入redis出错了");

            return false;
        }
    }

    /**
     * 获取hash键的所有map
     * @param key
     * @return
     */
    public Map<Object,Object> hget(String key){
        try{
            return redisTemplate.opsForHash().entries(key);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("放入redis出错了");
            return null;
        }
    }

    public Object hmget(String key,String field){
        try{
            return redisTemplate.opsForHash().get(key,field);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("放入redis出错了");

            return null;
        }
    }

    /**
     * HashSet 添加
     * @param key
     * @param data
     * @return
     */
    public boolean hset(String key, Map<Object,Object> data){
        try{
            redisTemplate.opsForHash().putAll(key,data);
            System.out.println("放入redis成功了");
            return true;
        }catch (Exception e){
            System.out.println("放入redis出错了");
            e.printStackTrace();
            return false;
        }
    }

    /**
     * HashSet 并设置过期时间
     * @param key
     * @param data
     * @param time
     * @return
     */
    public boolean hmset(String key, Map<Object,Object> data,long time){
        try{
            redisTemplate.opsForHash().putAll(key,data);
            if(time>0) {
                this.expire(key, time);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("放入redis出错了");

            return false;
        }
    }

    /**
     * 根据key和field往hash表里添加值
     * @param key
     * @param field
     * @param data
     * @return
     */
    public boolean hset(String key,String field,Object data){
        try{
            redisTemplate.opsForHash().put(key,field,data);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("放入redis出错了");
            return false;
        }
    }

    /**
     * 根据 HashSet 并设置过期时间
     * @param key
     * @param field
     * @param data
     * @param time
     * @return
     */
    public boolean hset(String key,String field,Object data,long time){
        try{
            redisTemplate.opsForHash().put(key,field,data);
            if(time>0){
                this.expire(key,time);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();          
            System.out.println("放入redis出错了");
            return false;
        }
    }

}
